package com.eliza.databind.activity.adapter

import android.graphics.Color
import androidx.databinding.BindingAdapter
import android.text.TextUtils
import android.widget.ImageView
import com.squareup.picasso.Picasso
import com.eliza.databind.R

/*-*- coding:utf-8 -*-
 * @Author  : debi
 * @Time    : 3/26/22
 * @Software: Android Studio
 */
object ImgBindAdapter {
    @JvmStatic
    @BindingAdapter("imgJava")
    fun setImg(iv: ImageView, url: String) {
        if (!TextUtils.isEmpty(url)) {
            Picasso.get()
                .load(url)
                .placeholder(R.drawable.ic_error)
                .into(iv)
        } else {
            iv.setBackgroundColor(Color.GRAY)
        }
    }

    @JvmStatic
    @BindingAdapter("imgJava")
    fun setImg(iv: ImageView, resId: Int) {
        iv.setImageResource(resId)
    }

    @JvmStatic
    @BindingAdapter(value = ["imgJava","defaultImg"], requireAll = false)
    fun setImg(iv: ImageView, url: String, resId: Int) {
        /*
        若 url 为空，则是用 本地图片
        * */
        if (!TextUtils.isEmpty(url)) {
            Picasso.get()
                .load(url)
                .placeholder(R.drawable.ic_error)
                .into(iv)
        } else {
            iv.setImageResource(resId)

        }
    }


}